package com.swz.dcrm.ui.mine;


import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.navigation.fragment.NavHostFragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bigkoo.pickerview.TimePickerView;
import com.google.gson.Gson;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.swz.dcrm.adpter.AnnouncementAdapter;
import com.swz.dcrm.adpter.GlobalAdapter;
import com.swz.dcrm.model.Announcement;
import com.swz.dcrm.model.BaseResponse;
import com.swz.dcrm.model.Page;
import com.swz.dcrm.ui.base.BaseFragment;
import com.swz.dcrm.R;

import android.widget.TextView;

import butterknife.BindView;
import butterknife.OnClick;

import com.swz.dcrm.R;
import com.swz.dcrm.ui.viewmodel.AnnouncementViewModel;
import com.swz.dcrm.util.CustomDecoration;
import com.swz.dcrm.wrapper.EmptyWrapper;
import com.xh.baselibrary.callback.OnClickListener;


import java.util.ArrayList;
import java.util.Date;

import javax.inject.Inject;

/**
 * A simple {@link Fragment} subclass.
 */
public class AnnouncementFragment extends BaseFragment {

    @BindView(R.id.toolbar_title)
    TextView title;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.smartRefreshLayout)
    SmartRefreshLayout smartRefreshLayout;
    @BindView(R.id.rv)
    RecyclerView rv;

    private int page = 1;
    private int size = 10;
    private int total;
    private EmptyWrapper emptyWrapper;
    private AnnouncementAdapter announcementAdapter;

    @Inject
    AnnouncementViewModel announcementViewModel;


    public static AnnouncementFragment newInstance() {

        return new AnnouncementFragment();
        // Required empty public constructor
    }

    @Override
    public void onLoadRetry() {
        if (!isNetworkConnected(getContext())) {
            mHolder.showLoadingStatus(GlobalAdapter.NET_ERROR);
        }
        announcementViewModel.findAnnouncement(page, size)
                .observe(this, observer);

    }
    Observer<BaseResponse<Page<Announcement>>> observer=new Observer<BaseResponse<Page<Announcement>>>() {
        @Override
        public void onChanged(BaseResponse<Page<Announcement>> pageBaseResponse) {

            if (pageBaseResponse.isSuccess()) {
                smartRefreshLayout.setEnableLoadmore(true);
                smartRefreshLayout.finishLoadmore();
                smartRefreshLayout.finishRefresh();
                total = pageBaseResponse.getData().getTotal();
                if (announcementAdapter==null) {
                    announcementAdapter = new AnnouncementAdapter(getContext(), pageBaseResponse.getData().getList());
                    announcementAdapter.setOnClickListener(onClickListener);

                    emptyWrapper=getEmptyWrapper(announcementAdapter,R.mipmap.empty_notice);
                    rv.setAdapter(emptyWrapper);

                } else {
                    announcementAdapter.refresh(pageBaseResponse.getData().getPageNum(),pageBaseResponse.getData().getList());
                    emptyWrapper.notifyDataSetChanged();
                }
            }
        }
    };

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);


    }

    @Override
    public int layoutId() {
        return R.layout.fragment_announcement;
    }

    @Override
    public boolean initView() {
        getDigger().inject(this);
        title.setText(getString(R.string.announcement_title));
        tvRight.setText(getString(R.string.announcement_add));
        rv.setLayoutManager(new LinearLayoutManager(getContext()));
        rv.addItemDecoration(new CustomDecoration(getContext(), 10, 0, 10, 10));
        smartRefreshLayout.setOnRefreshListener(refreshlayout -> {
            smartRefreshLayout.setEnableLoadmore(true);
            page=1;
            onLoadRetry();
            smartRefreshLayout.setEnableLoadmore(true);
        });
        smartRefreshLayout.setOnLoadmoreListener(refreshlayout -> {
            if (announcementAdapter != null && announcementAdapter.getItemCount() >= total) {
                smartRefreshLayout.finishLoadmore();
                smartRefreshLayout.setEnableLoadmore(false);
                return;
            } else {
                page++;
                onLoadRetry();
            }
        });
        announcementViewModel.getAddAnnouncement().observe(this, announcementBaseResponse -> {
            page=1;
            onLoadRetry();
            smartRefreshLayout.setEnableLoadmore(true);
        });
        announcementViewModel.getDelAnnouncement().observe(this, announcementBaseResponse -> {
            if(announcementBaseResponse.isSuccess()){
                page=1;
                onLoadRetry();
                smartRefreshLayout.setEnableLoadmore(true);
            }
        });
        onLoadRetry();
           return true;
    }


        @OnClick(R.id.iv_back)
        public void backClick () {
            NavHostFragment.findNavController(this).navigateUp();
        }
        @OnClick(R.id.tv_right)
        public void right () {
            go(null, R.id.action_announcementFragment_to_announceFragment, null);
        }

        OnClickListener<Announcement> onClickListener= announcement -> {
            AnnouncementDetailFragmentArgs.Builder builder=new AnnouncementDetailFragmentArgs.Builder();
            builder.setAnnouncement(new Gson().toJson(announcement));
            go(null, R.id.action_announcementFragment_to_announcementDetailFragment, builder.build().toBundle());
        };
}
